<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.type" placeholder="申请类型" clearable style="width: 150px;" class="filter-item">
        <el-option v-for="item in typeList" :key="item.key" :label="item.value" :value="item.key" />
      </el-select>
      <el-select v-model="listQuery.progress" placeholder="进度" clearable style="width: 150px;" class="filter-item">
        <el-option v-for="item in progressList" :key="item.key" :label="item.value" :value="item.key" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-setting" @click="handleOpenStatus">
        开通状态
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      highlight-current-row
      style="width: 100%;"
      :row-style="{height:'32px'}"
      tooltip-effect="light"
      :default-sort="{prop: 'progress', order: null}"
      @sort-change="sortChange"
    >
      <el-table-column label="编号" align="center" fixed width="80px" prop="applyId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.applyId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请类型" align="center" width="120px" prop="type" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" :type="typeMap[row.type].type"> {{ typeMap[row.type].label }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户ID" align="center" width="100px" prop="userId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" width="130px">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位" align="center" width="220px" show-overflow-tooltip>
        <template slot-scope="{row}">
          <span>{{ row.workplace }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" width="90px">
        <template slot-scope="{row}">
          <span>{{ genderMap[row.gender] }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" width="150px">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="疾病" align="center">
        <template slot-scope="{row}">
          <span>{{ row.disease }}</span>
          <span v-if="row.type===1">
            <el-tag v-if="row.isFixed===0" size="mini" effect="plain" type="info">其他疾病</el-tag>
            <el-tag v-if="row.isFixed===1" size="mini" effect="plain" type="primary">38种疾病</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="进度" align="center" prop="progress" sortable="custom" width="150px">
        <template slot-scope="{row}">
          <el-tag size="mini" effect="dark" :type="progressMap[row.progress].type"> {{ progressMap[row.progress].label }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发起时间" width="160px" align="center" prop="createTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleDetail(row)">
            详情
          </el-button>
          <el-button :disabled="row.progress!==0" type="success" size="mini" @click="handleApplyHandle(row.applyId)">
            处理
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showApplyMedicalList" />

    <el-dialog title="申请详情" :visible.sync="dialogDetailVisible" width="800px" :close-on-press-escape="false">
      <div style="margin: 20px">
        <el-row :gutter="20">
          <el-col :span="5" class="col-title"><p>申请编号</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ tempDetail.applyId }}</p></el-col>
          <el-col :span="5" class="col-title"><p>申请类型</p></el-col>
          <el-col :span="7" class="col-content">
            <p><el-tag size="mini" :type="typeMap[tempDetail.type].type"> {{ typeMap[tempDetail.type].label }} </el-tag></p>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5" class="col-title"><p>用户ID</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ tempDetail.userId }}</p></el-col>
          <el-col :span="5" class="col-title"><p>真实姓名</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ tempDetail.name }}</p></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5" class="col-title"><p>工作单位</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ tempDetail.workplace }}</p></el-col>
          <el-col :span="5" class="col-title"><p>性别</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ genderMap[tempDetail.gender] }}</p></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5" class="col-title"><p>身份证号</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ tempDetail.idNumber }}</p></el-col>
          <el-col :span="5" class="col-title"><p>手机号码</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ tempDetail.phone }}</p></el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5" class="col-title"><p>所患疾病</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ tempDetail.disease }}</p></el-col>
          <el-col :span="5" class="col-title"><p>审核进度</p></el-col>
          <el-col :span="7" class="col-content">
            <p><el-tag size="mini" effect="dark" :type="progressMap[tempDetail.progress].type"> {{ progressMap[tempDetail.progress].label }} </el-tag></p>
          </el-col>
        </el-row>
        <el-row v-if="tempDetail.type===1" :gutter="20">
          <el-col :span="5" class="col-title"><p>是否属于38类疾病</p></el-col>
          <el-col :span="7" class="col-content">
            <p v-if="tempDetail.isFixed===1">是</p>
            <p v-else>否</p>
          </el-col>
          <el-col :span="5" class="col-title"><p>是否为工伤</p></el-col>
          <el-col :span="7" class="col-content">
            <p v-if="tempDetail.isWorking===1">是</p>
            <p v-else>否</p>
          </el-col>
        </el-row>
        <el-row v-if="tempDetail.type===1" :gutter="20">
          <el-col :span="5" class="col-title"><p>本年是否住院</p></el-col>
          <el-col :span="7" class="col-content">
            <p v-if="tempDetail.isHospital===0">是</p>
            <p v-else>否</p>
          </el-col>
          <el-col :span="5" class="col-title"><p>首次住院日期</p></el-col>
          <el-col :span="7" class="col-content">
            <p v-if="tempDetail.hospitalDate">{{ tempDetail.hospitalDate }}</p>
            <p v-else>暂无</p>
          </el-col>
        </el-row>
        <el-row v-if="tempDetail.type===1" :gutter="20">
          <el-col :span="5" class="col-title"><p>入住医院全称</p></el-col>
          <el-col :span="7" class="col-content">
            <p v-if="tempDetail.hospitalName">{{ tempDetail.hospitalName }}</p>
            <p v-else>暂无</p>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5" class="col-title"><p>申请发起时间</p></el-col>
          <el-col :span="7" class="col-content"><p>{{ tempDetail.createTime }}</p></el-col>
          <el-col :span="5" class="col-title"><p>申请处理时间</p></el-col>
          <el-col :span="7" class="col-content">
            <p v-if="tempDetail.progress===0">暂未处理</p>
            <p v-else>{{ tempDetail.handleTime }}</p>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="5" class="col-title"><p>申请处理意见</p></el-col>
          <el-col :span="19" class="col-content">
            <p v-if="tempDetail.progress===0">暂未处理</p>
            <p v-else>{{ tempDetail.handleNote }}</p>
          </el-col>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDetailVisible = false">
          关闭
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核申请" :visible.sync="dialogVisible" width="600px">
      <el-form ref="dataForm" :rules="rules" :model="tempHandle" label-width="130px" style="width: 90%">
        <el-form-item label="审核结果" prop="progress">
          <el-select v-model="tempHandle.progress" style="width: 150px;" class="filter-item">
            <el-option :key="1" label="审核通过" :value="1" />
            <el-option :key="2" label="审核拒绝" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="处理意见" prop="handleNote">
          <el-input v-model="tempHandle.handleNote" type="textarea" :row="3" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="doApplyHandle">
          确认审核
        </el-button>
      </div>
    </el-dialog>

    <el-dialog title="申请开通状态" :visible.sync="dialogOpenStatusVisible" width="500px">
      <el-form ref="openStatusForm" :rules="openStatusRules" :model="tempOpenStatus" label-width="130px" style="width: 90%">
        <el-form-item label="重病住院申请" prop="zbzy1">
          <el-select v-model="tempOpenStatus.zbzy1" style="width: 150px;" class="filter-item">
            <el-option key="on" label="开通" value="on" />
            <el-option key="off" label="关闭" value="off" />
          </el-select>
        </el-form-item>
        <el-form-item label="大病救助申请" prop="dbjz2">
          <el-select v-model="tempOpenStatus.dbjz2" style="width: 150px;" class="filter-item">
            <el-option key="on" label="开通" value="on" />
            <el-option key="off" label="关闭" value="off" />
          </el-select>
        </el-form-item>
        <el-form-item label="持卡保障申请" prop="ckbz3">
          <el-select v-model="tempOpenStatus.ckbz3" style="width: 150px;" class="filter-item">
            <el-option key="on" label="开通" value="on" />
            <el-option key="off" label="关闭" value="off" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogOpenStatusVisible = false">
          关闭
        </el-button>
        <el-button type="primary" @click="setOpenStatus">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { handleApplyMedical, showApplyMedicalList, getApplyOpenStatus, setApplyOpenStatus } from '@/api/apply'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { isInteger } from '@/utils/validate'

export default {
  name: 'ApplyMedical',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      dialogLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        sortProp: '',
        sortOrder: '',
        type: undefined,
        progress: undefined,
        departmentId: ''
      },
      tempHandle: {
        applyId: undefined,
        progress: undefined,
        handleNote: ''
      },
      tempOpenStatus: {
        zbzy1: '',
        dbjz2: '',
        ckbz3: ''
      },
      tempDetail: {
        applyId: '',
        userId: '',
        type: 1,
        name: '',
        workplace: '',
        gender: 0,
        idNumber: '',
        phone: '',
        email: '',
        disease: '',
        isHospital: '',
        hospitalDate: '',
        progress: 0,
        handleTime: '',
        handleNote: '',
        createTime: '',
        isWorking: '',
        hospitalName: '',
        isFixed: ''
      },
      rules: {
        progress: [{ required: true, message: '请选择审核结果', trigger: 'change' }, { validator: isInteger, trigger: 'blur' }]
      },
      openStatusRules: {
        zbzy1: [{ required: true, message: '请选择重病住院申请开通状态', trigger: 'change' }],
        dbjz2: [{ required: true, message: '请选择大病救助申请开通状态', trigger: 'change' }],
        ckbz3: [{ required: true, message: '请选择持卡保障申请开通状态', trigger: 'change' }]
      },
      dialogVisible: false,
      dialogDetailVisible: false,
      dialogOpenStatusVisible: false,
      typeList: [
        { key: 1, value: '重病住院' },
        { key: 2, value: '大病救助' },
        { key: 3, value: '持卡保障' }
      ],
      typeMap: {
        1: { type: 'primary', label: '重病住院' },
        2: { type: 'danger', label: '大病救助' },
        3: { type: 'success', label: '持卡保障' }
      },
      progressList: [
        { key: 0, value: '待审核' },
        { key: 1, value: '审核通过' },
        { key: 2, value: '审核拒绝' }
      ],
      progressMap: {
        0: { type: 'primary', label: '待审核' },
        1: { type: 'success', label: '审核通过' },
        2: { type: 'danger', label: '审核拒绝' }
      },
      genderMap: {
        0: '未知',
        1: '男',
        2: '女'
      }
    }
  },
  created() {
    this.showApplyMedicalList()
  },
  methods: {
    showApplyMedicalList() {
      this.listQuery.departmentId = this.$store.getters.tmpDepartmentId
      this.listLoading = true
      showApplyMedicalList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showApplyMedicalList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    resetDetailDialog() {
      this.tempDetail = {}
    },
    resetOpenStatusDialog() {
      this.tempOpenStatus = {
        zbzy1: '',
        dbjz2: '',
        ckbz3: ''
      }
    },
    resetDialog() {
      this.tempHandle = {
        applyId: undefined,
        progress: undefined,
        handleNote: ''
      }
    },
    handleDetail(row) {
      this.resetDetailDialog()
      this.tempDetail = Object.assign({}, row)
      this.dialogDetailVisible = true
    },
    handleOpenStatus() {
      this.resetOpenStatusDialog()
      this.$nextTick(() => {
        this.$refs['openStatusForm'].clearValidate()
      })
      this.dialogOpenStatusVisible = true
      getApplyOpenStatus().then(response => {
        this.tempOpenStatus = {
          zbzy1: response.data.zbzy1,
          dbjz2: response.data.dbjz2,
          ckbz3: response.data.ckbz3
        }
      })
    },
    setOpenStatus() {
      this.$refs['openStatusForm'].validate((valid) => {
        if (valid) {
          setApplyOpenStatus(this.tempOpenStatus).then(() => {
            this.dialogOpenStatusVisible = false
            this.$notify({
              title: 'Success',
              message: '修改开放状态成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleApplyHandle(applyId) {
      this.resetDialog()
      this.tempHandle.applyId = applyId
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    doApplyHandle() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleApplyMedical(this.tempHandle).then(() => {
            this.showApplyMedicalList()
            this.dialogVisible = false
            this.$notify({
              title: 'Success',
              message: '审核申请成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>

<style>
  .col-title {
    text-align: right
  }

  .col-title p {
    font-weight: bolder;
  }

  .col-content p {
    color: #848383
  }
</style>
